﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Hosting.OpenXmlFileProjectStoreInternals
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Linq;

    internal class ProjectStoreItemCollection : IProjectStoreItemCollection
    {
        private readonly static XNamespace NS = OpenXmlFileProjectStore.NS;

        private readonly OpenXmlFileProjectStore mStore;
        private readonly XElement mItemsElement;
        private readonly List<IProjectStoreItem> mItems = new List<IProjectStoreItem>();

        #region .ctor

        internal ProjectStoreItemCollection(OpenXmlFileProjectStore store, XElement itemsElement)
        {
            Debug.Assert(itemsElement.Name == NS + "Items");

            // Load child items
            mItemsElement = itemsElement;

            foreach (var itemElement in itemsElement.Elements(NS + "Item"))
            {
                mItems.Add(new ProjectStoreItem(store, itemElement));
            }

            // Need the store for item creation
            mStore = store;
        }

        #endregion

        #region IProjectStoreItemCollection Members

        public int Count
        {
            get { return mItems.Count; }
        }

        public IProjectStoreItem this[int index]
        {
            get { return mItems[index]; }
        }

        public IProjectStoreItem Add(string itemName)
        {
            // Make sure there's not already an item with that name
            if (null != mItems.Find((c) => string.Equals(c.Name, itemName, StringComparison.OrdinalIgnoreCase)))
            {
                throw new ArgumentOutOfRangeException("itemName");
            }

            // Allocate a part for the item
            var part = mStore.AllocatePart(itemName);

            // Create a brand new item
            var itemElement = new XElement(NS + "Item");
            itemElement.SetAttributeValue("Name", itemName);
            itemElement.SetAttributeValue("ItemUri", part.Uri.ToString());
            mItemsElement.Add(itemElement);

            var item = new ProjectStoreItem(mStore, itemElement);
            mItems.Add(item);

            return item;
        }

        #endregion

        #region IEnumerable<IProjectStoreItem> Members

        IEnumerator<IProjectStoreItem> IEnumerable<IProjectStoreItem>.GetEnumerator()
        {
            return mItems.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return mItems.GetEnumerator();
        }

        #endregion

    }
}
